function getCurrentCases(){
	var lastDays = $("#portalTimeSelector option:selected").text();
	$.ajax({
		type: "GET",
		url: "../controller/readCases.php",
		data: "lastdays="+lastDays,
		
		beforeSend: function(xml){
			// zeige Wartegrafik??
		},
		
		success: function(xml){
			 $(xml).find('case').each(function(){
			 	var id = $(this).find('caseId').text();
				var topic = $(this).find('topic').text();
				var status = $(this).find('status').text();
				var editor = $(this).find('editor').text();
				var date = $(this).find('lastModified').text();
			 
			 	$('<div class="caseEntry" id="case_' + id + '" caseid="' + id + '"></div>').html("<b>" + topic + "</b><br/>" + editor + "<br/><i>" + date + "</i>").appendTo('#caseRoll');
			 });
		},
		
		complete: function(xml){
				$(".caseEntry").corner();
				$(".caseEntry").bind("click", function(){
					// get case details
					$.ajax({
						type: "GET",
						url: "../controller/readCases.php",												
						data: "case="+$(this).attr("caseid"),
						
						beforeSend: function(xml){
							// zeige Wartegrafik??
						},
						
						success: function(xml){
							// Append case details to message DIV
							var id = $(xml).find('caseId').text();
							var topic = $(xml).find('topic').text();
							var status = $(xml).find('caseStatus').text();
							var editor = $(xml).find('caseEditor').text();
							var date = $(xml).find('caseLastModified').text();
							
							$("#casePreview").empty();
							$("<div class='casePreviewContent'></div>").html("<p id='casePreviewInfo' caseid="+id+" class='casePreviewHead'>"+topic+"<br/>"+editor+"<br/>"+date+"<br/></p><p id='caseImagePreview'></p>").appendTo("#casePreview");
							
							$(xml).find('report').each(function(){
								var rId = $(this).find('reportId').text();
								var rHeadline = $(this).find('headline').text();
								var rDiagnosis = $(this).find('diagnosis').text();						
								var rText = $(this).find('reportText').text();
								// cut text to 500 characters - just a preview :-)
								rText = rText.substring(0,500);
								var rEditor = $(this).find('reportEditor').text();
								var rDate = $(this).find('reportLastModified').text();
										
								$("<p class='casePreviewReport' reportId="+rId+"></p>").html("<b>"+rHeadline+"<br/>"+rDiagnosis+"<br/></b><i>"+rEditor+" - "+rDate+"</i><br/><br/>"+rText+"[...]").appendTo("#casePreview");

							});
							
							$(xml).find('media').each(function(){
								var mediaId = $(this).find('mediaId').text();
								var mediaType = $(this).find('mediaType').text();
								var tags = $(this).find('tags').text();
								var modality = $(this).find('modality').text();
								var organ = $(this).find('organ').text();
								var thumbnail = $(this).find('thumbnail').text();
								var mediaLocator = $(this).find('mediaLocator').text();
								var mediaEditor = $(this).find('mediaEditor').text();
								var uploadDate = $(this).find('uploadDate').text();
								var windowCenter = $(this).find('windowCenter').text();
								var windowWidth = $(this).find('windowWidth').text();
								var laterality = $(this).find('laterality').text();
								var imageComment = $(this).find('imageComment').text();
								var contrast = $(this).find('contrast').text();
								var mrAcquisitionType = $(this).find('mrAcquisitionType').text();
								var sequenceName = $(this).find('sequenceName').text();
								var sliceThickness = $(this).find('sliceThickness').text();
								var kV = $(this).find('kV').text();
								var protocolName = $(this).find('protocolName').text();
								var kernel = $(this).find('kernel').text();
								var mrCoil = $(this).find('mrCoil').text();
								var flipAngle = $(this).find('flipAngle').text();
								var patPos = $(this).find('patPos').text();
								var sens = $(this).find('sens').text();
								var manufacturer = $(this).find('manufacturer').text();
								
								$("<span class='casePreviewImage'></span>").html("<img class='portalCasePreviewImage' src='"+thumbnail+"' alt='image' />").appendTo("#caseImagePreview");
							});
							
							var commentCount = $(xml).find('discussionList').children().size(); 
							var commentText = $("#casePreviewText1").text();
							var noCommentText = $("#casePreviewText2").text();
							
							if (commentCount == '0'){
								$("<p class='portalCaseCommentHeadline'></p>").html(commentCount+noCommentText).appendTo("#casePreview");
							}else{
								$("<p class='portalCaseCommentHeadline'></p>").html(commentCount+commentText).appendTo("#casePreview");
							}
							
							$(xml).find('comment:first-child').each(function(){
								var commentEditor = $(this).find('commentEditor').text();
								var commentText = $(this).find('commentText').text();
								var commentDate = $(this).find('commentDate').text();
								
								$("<p class='portalCaseComment'></p>").html("<b>"+commentEditor+":</b> "+commentText+"<br/><i>"+commentDate+"</i>").appendTo("#casePreview");
							});
						},
						
						complete: function(xml){
							// add a hover tooltip with the dicom params over the images?
						}
					});
					
					// Show message DIV
					$("#casePreview").dialog({
						bgiframe: true,
						modal: true,
						buttons: {
                            OK: function(){
                                $(this).dialog('close');
                            	$(this).dialog('destroy');
							},
							// Details: function(){
								// open single case view
							//	self.location.href='caseView.php?case='+$("#casePreviewInfo").attr("caseid");
							//},
							PDF: function(){
								window.open('../controller/pdf.php?case='+$("#casePreviewInfo").attr("caseid"));
							}
						},
					});
					
					/*
					 * Resize Case Preview Dialog to 60% width and height
					 */
					$('#casePreview').dialog('option', 'height', '90%');
					$('#casePreview').dialog('option', 'width', '80%');
					$('#casePreview').dialog('option', 'position', 'center');
				
				});
				
		}
	});
	
}

function caseFunctions(){
	/*
	 * Destroy Dialog Object on Close
	 */
	$('#casePreview').bind('dialogclose', function(event, ui) {
        $(this).dialog('destroy');
	});
}